/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufjf.posjava.cm.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Filipe
 */
public abstract class GenericDao<G,K> {
    
    public abstract void inserir(G obj) throws ClassNotFoundException, SQLException;
    public abstract void alterar(G obj) throws ClassNotFoundException, SQLException;
    public abstract void apagar(G obj) throws ClassNotFoundException, SQLException;
    public abstract G pesquisarChave(K obj) throws ClassNotFoundException, SQLException;
    public abstract ArrayList<G> buscarTodos() throws ClassNotFoundException, SQLException;
    
    /**
     * 
     * @param sql
     * @param param
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public ResultSet executaSql(String sql,List<String> param) throws ClassNotFoundException, SQLException{        
        Connection c = ConnectionFactory.getConnection();        
        PreparedStatement pst = c.prepareStatement(sql);
        int i = 1;
        for (String prm : param) {
            pst.setString(i++, prm);
        }        
        return pst.executeQuery();        
    }
    
    /**
     * 
     * @param sql
     * @param param
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public void executaDML(String sql,List<String> param) throws ClassNotFoundException, SQLException{        
        Connection c = ConnectionFactory.getConnection();        
        PreparedStatement pst = c.prepareStatement(sql);
        int i = 1;
        for (String prm : param) {
            pst.setString(i++, prm);
        }        
        pst.executeUpdate();        
    }    
}
